# Maintainer: Thomas Liske <thomas@fiasko-nw.net>
pkgname=pypy-bootstrap
pkgver=7.3.9
_py2ver=2.7.18
_pycpver=2.19
pkgrel=1
pkgdesc="A fast, compliant alternative implementation of Python"
url="https://www.pypy.org/"
arch="x86 x86_64 s390x aarch64 ppc64le"
license="MIT"
makedepends="
	bzip2-dev
	expat-dev
	gdbm-dev
	libc-dev
	libffi-dev
	linux-headers
	ncurses-dev
	openssl-dev
	perl
	pkgconf
	readline-dev
	rsync
	sqlite-dev
	tk-dev
	xz-dev
	zlib-dev
	"
options="!check"
# libpypy-c.so on x86 has textrels; no checks for bootstrap
[ "$CARCH" = "x86" ] && options="$options textrels"
source="https://downloads.python.org/pypy/pypy2.7-v$pkgver-src.tar.bz2
	https://www.python.org/ftp/python/$_py2ver/Python-$_py2ver.tar.xz
	https://files.pythonhosted.org/packages/source/p/pycparser/pycparser-$_pycpver.tar.gz
	py2_musl-find_library.py2patch
	py2_unchecked-ioctl.py2patch
	py2_cve-2021-3177.py2patch
	"

# Provide pypy-bootstrap for testing/pypy.
provider_priority=1 # lowest

prepare() {
	default_prepare

	cd Python-$_py2ver

	# apply Python2 patches
	for i in ../py2_*.py2patch; do
		patch -p1 <$i
	done

	# Make sure Python2 uses system libs
	rm -r Modules/expat Modules/_ctypes/libffi* Modules/zlib
}

build() {
	### Python2 ###
	export OPT="$CFLAGS -DTHREAD_STACK_SIZE=0x100000"
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=$srcdir/usr \
		--enable-ipv6 \
		--enable-optimizations \
		--enable-shared \
		--enable-unicode=ucs4 \
		--with-system-expat \
		--with-system-ffi \
		--with-system-zlib \
		--with-threads
	make LD_RUN_PATH="$srcdir/usr/lib"
	make -j1 install

	# pycparser
	cd $srcdir/pycparser-$_pycpver
	../usr/bin/python2 setup.py install


	### PyPy ###
	cd $srcdir/pypy2.7-v$pkgver-src/pypy/goal

	# translate with JIT
	[[ "$JOBS" -gt 16 ]] && export JOBS=16
	../../../usr/bin/python2 ../../rpython/bin/rpython \
		--opt=jit \
		--shared \
		--make-jobs $JOBS \
		targetpypystandalone

	# compile binary modules
	PYTHONPATH=../.. ./pypy-c ../../lib_pypy/pypy_tools/build_cffi_imports.py
}

package() {
	cd src/pypy2.7-v$pkgver-src

	install -Dm755 pypy/goal/pypy-c "$pkgdir"/usr/lib/pypy/bin/pypy-c
	install -Dm755 pypy/goal/libpypy-c.so "$pkgdir"/usr/lib/pypy/bin/libpypy-c.so

	rsync -r --exclude='__pycache__' --exclude='*.c' --exclude '*.o' lib_pypy/ "$pkgdir"/usr/lib/pypy/lib_pypy/

	rsync -r --exclude='__pycache__' --exclude='test' --exclude='tests' lib-python/ "$pkgdir"/usr/lib/pypy/lib-python/

	rsync -r --include='*.h' -f 'hide,! */' include/ "$pkgdir"/usr/lib/pypy/include/

	# Install symlinks
	mkdir -p "$pkgdir"/usr/bin "$pkgdir"/usr/lib
	ln -s ../lib/pypy/bin/pypy-c "$pkgdir"/usr/bin/pypy
	ln -s pypy/bin/libpypy-c.so "$pkgdir"/usr/lib/libpypy-c.so
}

sha512sums="
8d7513d4efe924f2456dd65a45a82dd16171f7962c81ba90f2a168c332cdad78ceffa3ed1af379c09e61082b39d5c2a8385cd77c89b698c47cc75e9df1bb4d2a  pypy2.7-v7.3.9-src.tar.bz2
a7bb62b51f48ff0b6df0b18f5b0312a523e3110f49c3237936bfe56ed0e26838c0274ff5401bda6fc21bf24337477ccac49e8026c5d651e4b4cafb5eb5086f6c  Python-2.7.18.tar.xz
7f830e1c9066ee2d297a55e2bf6db4bf6447b6d9da0145d11a88c3bb98505755fb7986eafa6e06ae0b7680838f5e5d6a6d188245ca5ad45c2a727587bac93ab5  pycparser-2.19.tar.gz
ab8eaa2858d5109049b1f9f553198d40e0ef8d78211ad6455f7b491af525bffb16738fed60fc84e960c4889568d25753b9e4a1494834fea48291b33f07000ec2  py2_musl-find_library.py2patch
5a8e013a4132d71c4360771f130d27b37275ae59330cf9a75378dc8a11236017f540eb224f2a148984e82ca3fb6b29129375b1080ba05b81044faa717520ab82  py2_unchecked-ioctl.py2patch
7d6c5d85a572450fb463feea9c7a8159aa8dd0c604e69f19da6eb57108c60e37ebce41b80758f63dd01e4a22286f80665ce4d778a30fa45ad5f1d6ce074c1f81  py2_cve-2021-3177.py2patch
"
